package flc.ast.dialog;

import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;

import flc.ast.R;
import flc.ast.databinding.DialogInputAnswerBinding;
import flc.ast.databinding.DialogSetAnswerBinding;
import stark.common.basic.base.BaseSmartDialog;

public class InputAnswerDialog extends BaseSmartDialog<DialogInputAnswerBinding> implements View.OnClickListener {

    public String question, answer;

    public InputAnswerDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean canceledOnTouchOutside() {
        return true;
    }

    @Override
    protected int getGravity() {
        return Gravity.CENTER;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.dialog_input_answer;
    }

    @Override
    protected void initView(View view) {
        mDataBinding.ivConfirm.setOnClickListener(this);

        mDataBinding.tvQuestion.setText(question);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ivCancel:
                dismiss();
                break;
            case R.id.ivConfirm:
                String text = mDataBinding.etAnswer.getText().toString().trim();
                if (TextUtils.isEmpty(text)) {
                    ToastUtils.showShort(R.string.please_input_answer);
                } else {
                    if (text.equals(answer)) {
                        dismiss();
                        if (listener != null) {
                            listener.clickConFirm();
                        }
                    } else {
                        ToastUtils.showShort(getContext().getString(R.string.answer_input_error));
                    }
                }
                break;
            default:
                break;
        }
    }

    private Ilistener listener;

    public void setListener(Ilistener listener) {
        this.listener = listener;
    }

    public interface Ilistener {
        void clickConFirm();
    }
}
